<template>
  <div class="page-shearch">
    <hy-form :formItems="searchConfing" v-model="formData">
      <template #header>
        <h3 class="header">世界和平</h3>
      </template>
      <template #footer>
        <div class="footer">
          <el-button type="primary" :icon="EditPen" @click="handlerResetClick">重置</el-button>
          <el-button type="success" :icon="Search" @click="handlerQueryClick">搜索</el-button>
        </div>
      </template>
    </hy-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue"
import hyForm from "@/base-ui/form"
import { Search, EditPen } from "@element-plus/icons-vue"
export default defineComponent({
  components: { hyForm },
  props: {
    searchConfing: {
      type: Object,
      require: true
    }
  },
  emits: ["changeBtnReset", "changeBtnSearch"],
  setup(props, { emit }) {
    // 双向绑定的属性由配置文件决定
    const formItems: any = props?.searchConfing ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ""
    }
    const formData = ref(formOriginData)
    // 重置按钮
    const handlerResetClick = () => {
      for (const item in formOriginData) {
        formData.value[item] = formOriginData[item]
      }
      emit("changeBtnReset")
    }
    // 用户搜索
    const handlerQueryClick = () => {
      emit("changeBtnSearch", formData.value)
    }

    return {
      formData,
      Search,
      EditPen,
      handlerResetClick,
      handlerQueryClick
    }
  }
})
</script>

<style lang="less" scoped>
.header {
  color: #0a0;
  margin-bottom: 10px;
}
.footer {
  text-align: right;
  margin-bottom: 10px;
}
</style>
